﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QuanLyPhongKham
{
    public partial class ucDmPhongKham : BaseUserControl
    {
        private DAL.AccessData m;
        private string sql = "";
        private DataTable dt = new DataTable();
        public ucDmPhongKham(DAL.AccessData acc)
        {
            InitializeComponent();
            m = acc;
            this.Title = "DM Phòng khám";
        }

        private void frmDmpk_Load(object sender, EventArgs e)
        {
            mabs.DisplayMember = "HOTEN";
            mabs.ValueMember = "MA";
            mabs.DataSource = m.get_data("select ma,hoten from " + m.user + ".dmbs order by hoten").Tables[0];
            mabs.SelectedIndex = 0;
            load_grid();
            AddGridTableStyle();
        }

        private void butMoi_Click(object sender, EventArgs e)
        {
            tenpk.Enabled = true;
            mabs.Enabled = true;
            tenpk.Text = "";
            ena_button(false);
            mapk.Text = m.get_capid_mapk.ToString().PadLeft(2,'0');
            tenpk.Focus();
        }

        private void butLuu_Click(object sender, EventArgs e)
        {
            if (tenpk.Text == "")
            {
                tenpk.Focus(); return;
            }
            if (mapk.Text != "")
            {
                m.upd_dmpk(mapk.Text, tenpk.Text, 0, mabs.SelectedValue.ToString(), "");
                tenpk.Enabled = false;
                mabs.Enabled = false; 
                load_grid();
                ena_button(true);
                reft_text();
                //butMoi.Focus();
            }
        }

        private void butSua_Click(object sender, EventArgs e)
        {
            ena_button(false);
            tenpk.Enabled = true;
            mabs.Enabled = true; 
            tenpk.Focus();
        }

        private void butBoqua_Click(object sender, EventArgs e)
        {
            ena_button(true);
            tenpk.Enabled = false;
            mabs.Enabled = false; 
        }

        private void butHuy_Click(object sender, EventArgs e)
        {
            if (mapk.Text != "" && tenpk.Text != "")
            {
                if (kiemtra(mapk.Text))
                {
                    MessageBox.Show("Danh mục đã sử dụng, không được huỷ!", DAL.AccessData.Msg); return;
                }
                if (MessageBox.Show("Bạn có đồng ý huỷ không?", DAL.AccessData.Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    sql = "delete from " + m.user + ".btdkp where makp='" + mapk.Text + "'";
                    m.execute_data(sql);
                    load_grid();
                }
            }
        }
        private bool kiemtra(string ma)
        {
            sql = "select * from " + m.user + ".benhanpk where makp='" + ma + "'";
            if (m.get_data(sql).Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }

        private void butKetthuc_Click(object sender, EventArgs e)
        {
            //fix:
            //this.Close();
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            reft_text();
            tenpk.Enabled = false;
        }
        private void reft_text()
        {
            //try
            //{
            //    tenpk.Enabled = false; 
            //    mapk.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 0].ToString();
            //    tenpk.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 1].ToString();
            //    mabs.SelectedValue = dataGrid1[dataGrid1.CurrentCell.RowNumber, 3].ToString();
            //}
            //catch { }
        }
        private void load_grid()
        {
            sql = "select * from " + m.user + ".btdkp order by makp";
            dt = m.get_data(sql).Tables[0];
            this.gridControl1.DataSource = dt;
        }private void AddGridTableStyle()
        {
            DataGridColoredTextBoxColumn TextCol;
            delegateGetColorRowCol de = new delegateGetColorRowCol(MyGetColorRowCol);
            DataGridTableStyle ts = new DataGridTableStyle();
            ts.MappingName = dt.TableName;
            ts.AlternatingBackColor = Color.Linen;
            ts.BackColor = Color.GhostWhite;
            ts.ForeColor = Color.MidnightBlue;
            ts.GridLineColor = SystemColors.Control;
            ts.HeaderBackColor = SystemColors.Control;
            ts.HeaderForeColor = Color.Navy;
            ts.SelectionBackColor = Color.Teal;
            ts.SelectionForeColor = Color.White;
            ts.ReadOnly = true;
            ts.RowHeaderWidth = 5;

            TextCol = new DataGridColoredTextBoxColumn(de, 0);
            TextCol.MappingName = "makp";
            TextCol.HeaderText = "Mã";
            TextCol.Width = 60;
            ts.GridColumnStyles.Add(TextCol);
            //dataGrid1.TableStyles.Add(ts);


            TextCol = new DataGridColoredTextBoxColumn(de, 1);
            TextCol.MappingName = "tenkp";
            TextCol.HeaderText = "Tên";
            //TextCol.Width = dataGrid1.Right - 97;
            ts.GridColumnStyles.Add(TextCol);
            //dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 2);
            TextCol.MappingName = "mavp";
            TextCol.HeaderText = "";
            TextCol.Width = 0;
            ts.GridColumnStyles.Add(TextCol);
            //dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 3);
            TextCol.MappingName = "mabs";
            TextCol.HeaderText = "";
            TextCol.Width = 0;
            ts.GridColumnStyles.Add(TextCol);
            //dataGrid1.TableStyles.Add(ts);
        }
        public Color MyGetColorRowCol(int row, int col)
        {
            Color c = Color.Navy;
            //try
            //{
            //    if (row == dataGrid1.CurrentRowIndex)
            //    {
            //        dataGrid1.Select(row);
            //        c = Color.White;
            //    }
            //}
            //catch
            //{
            //}
            return c;
        }
        public delegate Color delegateGetColorRowCol(int row, int col);
        public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
        {
            private delegateGetColorRowCol _getColorRowCol;
            private int _column;
            public DataGridColoredTextBoxColumn(delegateGetColorRowCol getcolorRowCol, int column)
            {
                _getColorRowCol = getcolorRowCol;
                _column = column;
            }
            protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
            {
                try
                {
                    foreBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));
                }
                catch { }
                finally
                {
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
                }
            }
        }
        private void ena_button(bool ena)
        {
            //butMoi.Enabled = ena;
            //butLuu.Enabled = !ena;
            //butSua.Enabled = ena;
            //butHuy.Enabled = ena;
            //butBoqua.Enabled = !ena;
            //butKetthuc.Enabled = ena;
        }

        private void hoten_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}{F4}");
            }
        }

        private void crudOperation1_ButtonClick(object sender, global::QuanLyPhongKham.DBOperateEventArgs e)
        {
            switch (e.ButtonKind)
            { 
                case global::QuanLyPhongKham.ButtonType.New:
                    butMoi_Click(null, null);
                    break;
                case global::QuanLyPhongKham.ButtonType.Edit:
                    butSua_Click(null, null);
                    break;
                case global::QuanLyPhongKham.ButtonType.Delete:
                    butHuy_Click(null, null);
                    break;
                case global::QuanLyPhongKham.ButtonType.Save:
                    butLuu_Click(null, null);
                    break;
                case global::QuanLyPhongKham.ButtonType.Ignore:
                    butBoqua_Click(null, null);
                    break;
                default:
                    break;

            }

        }
    }
}